import { goodsList, goodsCount } from "@/request/api"


const state = {
    goodslist: [],
    size: 2,
    page: 1,
    count: 0
}

const getters = {
    getGoodsList(state) {
        return state.goodslist
    },
    getCount(state) {
        return state.count
    },
    getSize(state) {
        return state.size
    }
}

const mutations = {
    REQ_GOODSLIST(state, payload) {
        state.goodslist = payload
    },
    REQ_GETCOUNT(state, payload) {
        state.count = payload
    },
    REQ_PAGE(state, payload) {
        state.page = payload
    }
}

const actions = {
    changeGoodsListAction(context) {
        console.log(context, '111111');
        goodsList({
            size: context.state.size,
            page: context.state.page,
        }).then(res => {
            console.log(res, '商品管理列表');
            if (res.code == 200) {
                context.commit('REQ_GOODSLIST', res.list)
                let list = res.list ? res.list : []
                if (context.state.page != 1 && list.length == 0) {
                    context.dispatch('changePageAction', context.state.page - 1)
                }
            }
        })
    },
    changeCountAction({ commit }) {
        goodsCount().then(res => {
            console.log(res, '总数');
            if (res.code == 200) {
                commit('REQ_GETCOUNT', res.list[0].total)
            }
        })
    },
    changePageAction(context, payload) {
        context.commit('REQ_PAGE', payload)
        context.dispatch('changeGoodsListAction')
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}